<?php
/**
 * Created by PhpStorm.
 * User: liziang
 * Date: 2019/1/3
 * Time: 14:57
 */
namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\PlayerForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Grid;
use Illuminate\Http\Request;
use \Illuminate\Support\Facades\Input;
use App\Admin\Extra\Search\Html;
use App\Models\Area;
use Redirect;

class PlayerFormController extends Controller{

    public function anyListAccount()
    {
        $req_param = Input::all();
        //OrderForm::getAccountByName();
        $use_data['area_all'] = Area::areaSelectData(0);
        $use_data['area_online'] = Area::areaSelectData();

        return Admin::content(function (Content $content)use($req_param, $use_data) {

            $content->header('player form');
            $content->description('订单列表--根据玩家ID');

            $content->row(Html::append([
                Html::input('cp_player_id', 'cp_player_id', url()->current()),
                Html::button('GO', url()->current(), 'search-filter'),
                Input::get('charge_ok', 1) ? Html::button('Charge OK', \App\Service\Common::build_url_query(url()->current(), ['charge_ok' => 0]), 'btn-goto btn-info')
                : Html::button('Precharge', \App\Service\Common::build_url_query(url()->current(),  ['charge_ok' => 1]), 'btn-goto btn-warning'),
                Html::sjs(url()->current())
            ]));


            $grid = Admin::grid(PlayerForm::class, function(Grid $grid) use ($use_data) {

//                $grid->model()->where('deleted_at', 0);
                $grid->actions(function($actions){
                    $actions->disableDelete();
                    $actions->disableEdit();
                    $actions->append('<a href="'.url('admin/playerorder/list-all?account='). $actions->row->cp_account .'&area='.$actions->row->cp_area.'"><i class="glyphicon glyphicon-eye-open"></i></a>');
                });
                $created_time = Input::get('created_time');
                $grid->date('Date [from ~ to]')->display(function()use ($created_time) {
                    if (empty($created_time) ) {
                        return 'All';
                    }
                    return substr($created_time['start'],0, 10) . ' ~ ' . substr($created_time['end'], 0, 10);
                });
                $grid->created_acc_time('Account Create Time');
                // 第一列显示id字段，并将这一列设置为可排序列
                $grid->cp_account('cp_account');
                $grid->model()->orderBy('cp_account', 'DESC');

                // 第二列显示title字段，由于title字段名和Grid对象的title方法冲突，所以用Grid的column()方法代替
                $grid->column('cp_player_id');
                // $grid->column('cp_curr_server');
                $grid->cp_area('cp_area')->display(function($value)use($use_data) {
                    return $use_data['area_all'][$value] . "(${value})";
                });
                $grid->total('Charge Count')->display(function($value)use($created_time){
                    $d_url = \App\Service\Common::build_url_query(url('admin/playerorder/list-all'), [
                        'account' => $this->cp_account,
                        'area' => $this->cp_area,
                        'created_time' => $created_time,
                    ]);
                    return '<a href="'.$d_url.'">'.$value.'</i></a>';
                });
                $grid->sum('Total Cost')->display(function($value){
                    return "$ ${value}";
                });
                

                $grid->filter(function ($filter) use($use_data){

                    $filter->like('cp_player_id', 'cp_player_id')->placeholder('');
                    $filter->equal('cp_area')->select($use_data['area_online']);
                    $filter->between('created_time', 'Created Time')->datetime();

                });
            });
            $content->row($grid->render());
        });


    }

    public function anyShow(Request $request) {
        $accountId = $request->get('id');

        $info = PlayerForm::detailPlayer($accountId);
        $data = [
            'title_act' => 'account operator',
        ];
        $data = array_merge($data, $info);
        return Admin::content(function (Content $content) use($data){
            $content->header('订单详情');
            $content->description('.....');

            $content->row(view('admin.orderform.playerform_show', $data?:[]));

        });
    }

}